package com.archerdu.apply.controller.front;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.archerdu.apply.dao.OptionDAO;
import com.archerdu.apply.model.Examin;
import com.archerdu.apply.model.Option;
import com.archerdu.apply.model.Question;
import com.archerdu.apply.service.ExaminManager;
import com.archerdu.apply.service.QuestionManager;
import com.archerdu.base.controller.BaseController;
import com.archerdu.commons.helper.examin.ExaminHelper;
import com.archerdu.commons.helper.examin.ScoreInfo;

@Controller("frontExaminController")
@Scope("prototype")
@RequestMapping("/examin")
public class ExaminController extends BaseController {

    private static final long serialVersionUID = -1234935879735436642L;
    private QuestionManager questionManager;
    private ExaminManager examinManager;
    
    /**
     * 查看试卷
     * @param id
     * @return
     */
    @RequestMapping("/{id}")
    public ModelAndView examin(@PathVariable Integer id){
        ModelAndView mav = new ModelAndView();
        
        Examin examin = examinManager.get(id);
        List<Question> list = questionManager.findByExaminId(id);
        for(Question que : list){
            // 去掉多于指定数量的备选答案
            if(que.getOptions().size() > que.getCount()){
                int count = que.getOptions().size() - que.getCount();
                for(Option opt : que.getOptions()){
                    if(opt.getIsright().equals( OptionDAO.ISRIGHT_WRONG)){
                        opt.setStat( OptionDAO.STATE_HIDDEN );
                        count--;
                    }
                    if(count <= 0){
                        break;
                    }
                }
            }
        }
        mav.addObject("examin", examin);
        mav.addObject("list", list);
        mav.addObject("message", getMessage());
        mav.setViewName("front/examin/list");
        return mav;
    }

    @RequestMapping("/achieve")
    public ModelAndView achieve(HttpServletRequest request, Integer id){
        ModelAndView mav = new ModelAndView();
        Integer score = 0;
        if( null != id ) {
            List<Question> questionList = questionManager.findByExaminId( id );
            int count = 0;
            for(Question que : questionList){
                String[] options = request.getParameterValues( que.getId().toString() );
                logger.info( que.getId() + "\n\r### "
                +  questionManager.checkOption( que, options ) );
                if(questionManager.checkOption( que, options ))
                    count++;
            }
            score = Math.round( (count * 100 / questionList.size()));
            
            Examin examin = examinManager.get( id );
            if(score == 100){
                addMessage(examin.getSucNotice());
            }
            
            List<ScoreInfo> list = ExaminHelper.toList(examin.getScoreSection());
            for(ScoreInfo si : list){
                if(score.compareTo(si.getScore()) >= 0){
                    addMessage(si.getInfo());
                    break;
                }
            }
            // 低于最低分数的记录
            if(null == getMessage().getInfo()){
                addMessage(examin.getFailNotice());
            }
            /* TODO 记录分数，需修改
            if(score > 0){
                User user = (User)session.get(Constant.SESSION_COMMON_USER);
                UserExamin userExamin = new UserExamin();
                userExamin.setExaminId(id);
                userExamin.setUserId(user.getId());
                
                List<UserExamin> list = userExaminManager.getAll(userExamin);
                if(list.size() > 0){
                    userExamin = list.get(0);
                    userExamin.setUpdatetime(getSqlDate());
                    userExamin.setUpdateUser(user.getId());
                    userExamin.setScore(score);
                }else{
                    userExamin.setCreatetime(getSqlDate());
                    userExamin.setCreateUser(user.getId());
                    userExamin.setScore(score);
                }
                userExaminManager.add(userExamin);
            }
            */
        }
        mav.addObject("score", score);
        mav.addObject("message", getMessage());
        mav.setViewName("front/examin/achieve");
        return mav;
    }
    public QuestionManager getQuestionManager() {
        return questionManager;
    }

    public void setQuestionManager(QuestionManager questionManager) {
        this.questionManager = questionManager;
    }

    public ExaminManager getExaminManager() {
        return examinManager;
    }

    public void setExaminManager(ExaminManager examinManager) {
        this.examinManager = examinManager;
    }
    
}
